﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interpolation2D
{
    class Vector2D
    {
        public Vector2D(double x, double y)
        {
            X = x;
            Y = y;
        }

        public double X { get; set; }
        public double Y { get; set; }
    }

    class Matrix2D
    {
        public double A11 { get; set; }
        public double A12 { get; set; }
        public double A21 { get; set; }
        public double A22 { get; set; }

        public Matrix2D(double a11, double a12, double a21, double a22)
        {
            A11 = a11;
            A12 = a12;
            A21 = a21;
            A22 = a22;
        }

        public double Det => A11 * A22 - A12 * A21;

        /// <summary>
        /// Получение обратной матрицы.
        /// </summary>
        public Matrix2D Invert
        {
            get
            {
                double d = Det;

                if (Math.Abs(d) < 0.0001)
                {
                    throw new Exception("Невозможно вычислить обратную матрицу.");
                }

                double a11 = A22 / d;
                double a12 = -A21 / d;
                double a21 = -A12 / d;
                double a22 = A11 / d;

                return new Matrix2D(a11, a12, a21, a22);
            }
        }

        public static Vector2D operator *(Matrix2D matrix, Vector2D vector)
        {
            double a1 = matrix.A11 * vector.X + matrix.A12 * vector.Y;
            double a2 = matrix.A21 * vector.X + matrix.A22 * vector.Y;

            return new Vector2D(a1, a2);
        }
    }

    class LinearSolver
    {
        /// <summary>
        /// Решение линейной системы 2х2.
        /// </summary>
        /// <param name="A">Матрица коэффициентов.</param>
        /// <param name="b">Вектор значений.</param>
        /// <returns>Вектор-решение.</returns>
        static public Vector2D Solve2x2(Matrix2D A, Vector2D b)
        {
            return A.Invert * b;
        }
    }
}
